'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 铁匠铺
 * @author sundbcn@gmail.com (sun debing)
 */

var dataApi = require('../libs/dataApi');
var _ = require('lodash');

// /**
//  * 获取 工人开启限制等级
//  * @param  {[type]} smithy_id [description]
//  * @return {[type]}     [description]
//  */
// exports.getOpenLv = function (worker_id) {
//     var smithyConf = dataApi.smithy_worker.findById(worker_id);
//     if (!smithyConf) {
//         return false;
//     }
//     return smithyConf.hero_lvl;
// };

/**
 * 通过建筑等级获取可以开放的工人选择
 * @param  {[type]} smithy_id [description]
 * @return {[type]}     [description]
 */
exports.getOpenLv = function (building_lvl) {
    var worker_list = [];
    var smithyConf = dataApi.smithy_worker.all();
    _.each(smithyConf, function (value) {
        if (value.building_lvl <= building_lvl) {
            worker_list.push(value.id);
            return true;
        } else {
            return false;
        }
    });
    return worker_list;
};

/**
 * 根据工人的ID 获取对应时间的产出最大和最小产出数量
 * @param  {[type]} smithy_id [description]
 * @param  {[type]} production_time [description]
 * @return {[type]}     [description]
 */
exports.getProductionRange = function (worker_id, production_time) {
    var smithy_item = dataApi.smithy_num.findById(worker_id);
    var item = {};
    if (!smithy_item) {
        console.error('smithy by worker_id and production_time -- null error ');
        return;
    }

    _.each(smithy_item, function (value) {
        if (value.time_cost == production_time) {
            item = value;
            return true;
        }
    });
    return item;
};

/**
 * 根据工人的ID 和时间获取 精力消耗
 * @param  {[type]} smithy_id [description]
 * @param  {[type]} production_time [description]
 * @return {[type]}     [description]
 */
exports.getEnergy = function (worker_id, production_time) {
    var smithy_item = dataApi.smithy_num.findById(worker_id);
    var item = {};
    if (!smithy_item) {
        console.error('smithy by worker_id and production_time -- null error ');
        return;
    }

    _.each(smithy_item, function (value) {
        if (value.time_cost == production_time) {
            item = value.energy_cost;
            return true;
        }
    });
    return item;
};

/**
 * 根据铁匠ID 获取对应钻石消耗
 * @param  {[type]} smithy_id [description]
 * @param  {[type]} production_time [description]
 * @return {[type]}     [description]
 */
exports.getPoint = function (smithy_id) {
    var smithy_conf = dataApi.smithy_tool.findById(smithy_id);
    if (!smithy_conf) {
        console.error('smithy_conf type can not found:' + smithy_id);
        return false;
    }
    return smithy_conf.point_cost;
};

/**
 * 根据铁匠ID 获取对应生产时间 时间验证
 * @param  {[type]} smithy_id [description]
 * @param  {[type]} production_time [description]
 * @return {[type]}     [description]
 */
exports.getWorkerTime = function (worker_id, worker_time) {
    var worker_conf = dataApi.smithy_num.findById(worker_id);
    var isVail = false;
    if (!worker_conf) {
        console.error('worker_conf type can not found:' + worker_id);
        return false;
    }
    _.each(worker_conf, function (value) {
        var time = value.time_cost;
        if (worker_time == time) {
            isVail = true;
            return true;
        }
    });
    return isVail;
};

/**
 * 获取铁匠铺概率信息
 * @param  {[type]} smithy_id [description]
 * @param  {[type]} production_time [description]
 * @return {[type]}     [description]
 */
exports.getProbabilityInfo = function (smithy_id) {
    var smithy_pro_list = [];
    var list = [];
    var total_prob = 0;
    var random_only = parseInt(random);
    var smithy_conf = dataApi.smithy_goods.findById(smithy_id);

    _.each(smithy_conf, function (value) {
        var item = _.clone(value, true);
        total_prob += value.probability;
        item.total_prob = total_prob;
        smithy_pro_list.push(item);
    });

    var random = _.random(0, total_prob - 1);
    _.some(smithy_pro_list, function (value) {
        var item = _.clone(value, true);

        if (item.total_prob >= random) {
            list.push(item);
            return true;
        }
    });
    return list;
};
